// pages/center/center.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否登录了
    isLogin: false,
    // 用户信息
    userInfo: {},
    // 是否有活动功能
    hasActivity: false,
    // 是否有相册
    hasAlbum: app.globalData.config['agentPlugins']['album']['status'],
    // 菜单列表
    menuList: [{
      //   iconUrl: '/images/agent/center/index/01.png',
      //   label: '留言',
      //   path: '/pages/agent/center/feedback/list/index'
      // }, {
      iconUrl: '/images/agent/center/index/02.png',
      label: '收藏',
      path: '/pages/agent/plugins/activity/collection/index'
    }, {
      iconUrl: '/images/agent/center/index/03.png',
      label: '点赞',
      path: '/pages/agent/plugins/activity/like/index'
    }, {
      iconUrl: '/images/agent/center/index/04.png',
      label: '评论',
      path: '/pages/agent/plugins/activity/comment/index'
    }],

    // 菜单栏
    navbarList: [{
      id: 167,
      iconUrl: '/images/agent/center/index/14.png',
      label: '申请入会',
      path: '/pages/agent/center/authentication/index'
    }, {
      id: 166,
      iconUrl: '/images/agent/center/index/08.png',
      label: '活动管理',
      path: '/pages/agent/plugins/activity/mine/index'
    }, {
      id: 178,
      iconUrl: '/images/agent/center/index/08.png',
      label: '订单管理',
      path: '/pages/agent/plugins/business/order/order'
    }, {
      id: -1,
      iconUrl: '/images/agent/center/index/08.png',
      label: '腾讯云订单',
      path: '/pages/agent/plugins/tencentCloud/order/order'
    }, {
      id: 177,
      iconUrl: '/images/agent/center/index/08.png',
      label: '我的钱包',
      path: '/pages/agent/plugins/wallet/record/record'
    }, {
      id: 214,
      iconUrl: '/images/agent/center/index/16.png',
      label: '我的反馈',
      path: '/pages/agent/plugins/feedback/mine/index'
    }, {
      id: 252,
      iconUrl: '/images/agent/center/index/16.png',
      label: '我的反馈',
      path: '/pages/agent/plugins/superWorkOrder/mine/index'
    }, {
      id: 0,
      iconUrl: '/images/agent/center/index/05.png',
      label: '使用帮助',
      path: '/pages/agent/center/useHelp/useHelp'
    }, {
      id: 0,
      iconUrl: '/images/agent/center/index/07.png',
      label: '关于我们',
      path: '/pages/agent/center/about-us/about-us'
    }, {
      id: 0,
      iconUrl: '/images/agent/center/index/06.png',
      label: '系统设置',
      path: '/pages/agent/center/setting/setting'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let navbarListTemp = [];
    let navbarList = this.data.navbarList;
    let pluginList = wx.getStorageSync('pluginList');
    for (let j in navbarList) {
      for (let i in pluginList) {
        if ((pluginList[i]['auth_id'] === navbarList[j]['id'] && pluginList[i]['display'] === 1) || navbarList[j]['id'] === 0) {
          if (navbarList[j]['id'] !== 0 && pluginList[i]['auth_id'] === 166) {
            this.setData({
              hasActivity: true
            })
          }
          navbarListTemp.push(navbarList[j]);
          break;
        }
      }
    }
    console.log(navbarListTemp);

    this.setData({
      navbarList: navbarListTemp
    })
    this.loginDialog = this.selectComponent("#login-dialog");
  },

  // 跳转页面
  bindJumpToPage(e) {
    var page = e.currentTarget.dataset.page;
    app.jumpToPage(page.path, page.name);
  },

  // 点击登录注册
  bindLogin() {
    this.loginDialog.showLoginDialog();
  },

  getUserInfo: function() {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getMemberDetail
    };
    app.server.getConnect(data).then(function(res) {
      app.globalData.userInfo = res.data;
      that.setData({
        userInfo: res.data,
        wxUserInfo: app.globalData.wxUserInfo
      })
    }, function() {
      app.globalData.userInfo = null;
      that.setData({
        userInfo: {},
        isLogin: false
      })
    })
  },

  // 跳转个人信息
  toPersonalInfo: function() {
    wx.navigateTo({
      url: '../personal/personal',
    })
  },

  // 跳转相册页
  toAlbumList() {
    wx.navigateTo({
      url: '/pages/agent/center/album/index?id=' + this.data.userInfo.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      hasAlbum: app.globalData.config['agentPlugins']['album']['status']
    })
    if (app.globalData.userInfo && app.globalData.userInfo.id) {
      this.getUserInfo();
      this.setData({
        isLogin: true
      })
    } else {
      this.setData({
        userInfo: {},
        isLogin: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.loginDialog.hideLoginDialog();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getUserInfo();
    app.utils.initAgentWxappStyle(app, this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})